var app = getApp()
Page({
    data: {
        region: ['请选择', '请选择', '请选择'],
        customItem: '全部',
        name: 'data.name',
        phone: '',
        area: '',
        address: '',

        isChecked: false,
        status: 0,
        id: '',

		address_type:'',		//地址类型

		is_submit:false,		// 是否提交
    },
    bindRegionChange(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            area: e.detail.value.join(''),
            region: e.detail.value
        })
    },
    valName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    valPhon(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    valAddress(e) {
        this.setData({
            address: e.detail.value
        })
    },
    changeSwitch: function(e) {
        this.setData({
            status: e.detail.value
        })
    },
    onLoad: function(options) {
        this.setData({
            name: options.name,
            phone: options.phone,
            area: options.area,
            address: options.address,
            status: options.status,
            id: options.id,
			address_type: options.type ? options.type : "",
        })
    },
    address: function() {
        var that = this;
		var address_type = this.data.address_type;
		var is_submit = this.data.is_submit;
		if (!is_submit) {
			this.setData({is_submit : true});
			wx.getStorage({
				key: 'user',
				success: function(res) {
					wx.request({
						url: app.url.requestUrl + '/personal/updateaddress',
						method: 'POST',
						data: {
							openid: res.data.openid,
							token: res.data.token,
							name: that.data.name,
							phone: that.data.phone,
							area: that.data.area,
							address: that.data.address,
							status: that.data.status,
							id: that.data.id,
						},
						success: function(res) {
							if(res.data.code == 1){
								wx.showToast({ title: res.data.msg })
								
								if (status && address_type == 'select') {
									wx.navigateBack({
										delta: 2
									})
								} else {
									wx.navigateBack({
										delta: 1
									})
								}
							} else {
								that.setData({is_submit : false})
								wx.showToast({
									image:'../../img/warning.png',
									title: res.data.msg,
								})
							}
						}
					})
				}
			})
		}
    }
})